﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Data;
using System.Data.SqlClient;
using Class_Entity;
using System.Web.Configuration;

namespace DataAccess
{
    public class DAEncapsulation
    {
        SqlConnection con;
        SqlCommand cmd;
        

        public bool Add_Encapsulation(Encapsulation encap)
        {
            try
            {
                con = ConnectionToDataBase.StartConnection();    
                con.Open();

                cmd = new SqlCommand("spAddEncapsulation", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@Name", encap.Model);
                cmd.Parameters.AddWithValue("@Out", encap.Output);
                cmd.Parameters.AddWithValue("@Image", encap.Image);
                cmd.Parameters.AddWithValue("@Size", encap.Size);
                cmd.Parameters.AddWithValue("@Dimension", encap.Dimension);
                cmd.Parameters.AddWithValue("@Weigth", encap.Weight);
                

                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return true;
        }

        //-----------------------------------------------------------------------------------

        public bool Edit_Encapsulation(Encapsulation encap)
        {
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spEditEncapsulation", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@ID", encap.ID);
                cmd.Parameters.AddWithValue("@Name", encap.Model);
                cmd.Parameters.AddWithValue("@Out", encap.Output);
                cmd.Parameters.AddWithValue("@Image", encap.Image);
                cmd.Parameters.AddWithValue("@Size", encap.Size);
                cmd.Parameters.AddWithValue("@Dimension", encap.Dimension);
                cmd.Parameters.AddWithValue("@Weigth", encap.Weight);


                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return true;
        }

        //-------------------------------------------------------------------------------------------

        public bool Delete_Encapsulation(Encapsulation encap)
        {
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spDeleteEncapsulation", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@ID", encap.ID);              
                
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return true;
        }


        //---------------------------------------------------------------------------------------------

        public DataSet Find_Encapsulation(Encapsulation encap)
        {
            DataSet ds = new DataSet();
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spFindEncapsulation", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@ID", encap.ID);               
                
                cmd.ExecuteNonQuery();

                data.Fill(ds);
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return ds;
        }

        //------------------------------------------------------------------------------------------------

        public DataSet Load_Encapsulation(Encapsulation encap)
        {
            DataSet ds = new DataSet();
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spLoadEncapsulation", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);                

                cmd.ExecuteNonQuery();
                
                data.Fill(ds);
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return ds;
        }


        //---------------------------------------------------------------------------------------------

        public DataSet Search_Encapsulation(Encapsulation encap)
        {
            DataSet ds = new DataSet();
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spSearchEncapsulation", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@name", encap.Model);

                cmd.ExecuteNonQuery();

                data.Fill(ds);
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return ds;
        }


        //---------------------------------------------------------------------------------------------

        public DataSet Detail_Encapsulation(Encapsulation encap)
        {
            DataSet ds = new DataSet();
            try
            {
                con = ConnectionToDataBase.StartConnection();
                con.Open();

                cmd = new SqlCommand("spDetailEncapsulation", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(cmd);

                cmd.Parameters.AddWithValue("@ID", encap.Model);

                cmd.ExecuteNonQuery();

                data.Fill(ds);
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDataBase.CloseConnection();
            }

            return ds;
        }
    }
}
